package com.chao.yyks.modular.config.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chao.yyks.modular.config.bean.Dic;
import com.chao.yyks.modular.config.service.DicService;
import com.chao.common.util.AddressConstant;
import com.chao.common.util.log.Inter;
import com.chao.common.util.reaction.PageList;
import com.chao.common.util.reaction.Reaction;
import com.chao.common.util.reaction.ReactionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 数据字典Controller
 * chao
 * 2020-08-05
 */
@RestController
@RequestMapping("/dic")
public class DicController {

	/**
	 * 数据字典Service
	 */
	@Autowired
	private DicService dicService;

	/**
	 * 新增数据字典
	 */
	@Inter("DICS_ADD_01")
	@RequestMapping(method = RequestMethod.POST, value = "insertDic")
	public void insertDic(@RequestBody Dic dic){
		dicService.insertDic(dic);
	}

	/**
	 * 修改数据字典
	 */
	@Inter("DIC_EDT_01")
	@RequestMapping(method = RequestMethod.POST, value = "updateDic")
	public void updateDic(@RequestBody Dic dic){
		dicService.updateDic(dic);
	}

	/**
	 * 同步数据
	 */
	@Inter(value = "DIC_SYN_01", grade = "1", answer = false)
	@RequestMapping(method = RequestMethod.GET, value = "synchro")
	public void synchro(){
		dicService.synchro();
	}

	/**
	 * 查询数据字典详情
	 */
	@Inter("DIC_SEL_01")
	@RequestMapping(method = RequestMethod.GET, value = "selectDicByDicId")
	public Dic selectDicByDicId(String dicId){
		return dicService.selectDicByDicId(dicId);
	}

	/**
	 * 查询数据字典列表
	 */
	@Inter(value = "DIC_SEL_02")
	@RequestMapping(method = RequestMethod.GET, value = "selectDicByList")
	public JSONObject selectDicByList(String dicType){
		return dicService.selectDicByList(dicType);
	}

	/**
	 * 分页查询数据字典列表
	 */
	@Inter("DIC_SEL_03")
	@RequestMapping(method = RequestMethod.GET, value = "selectDicByPage")
	public PageList<Dic> selectDicByPage(@RequestParam Map<String,Object> map){
		return dicService.selectDicByPage(new JSONObject(map));
	}

	/**
	 * 查询数据字典类型列表
	 */
	@Inter(value = "DIC_SEL_04", grade = "1")
	@RequestMapping(method = RequestMethod.GET, value = "selectDicTypeByList")
	public List<JSONObject> selectDicTypeByList(){
		return dicService.selectDicTypeByList();
	}

	/**
	 * 获取省市区数据
	 */
	@Inter(value = "DIC_SEL_05", grade = "1")
	@RequestMapping(method = RequestMethod.GET, value = "selectCity")
	public JSONArray selectCity() {
		return AddressConstant.address.getJSONArray("data");
	}

	/**
	 * 省市区接口
	 */
	@Inter(value = "SYS_SEL_02", grade = "1")
	@RequestMapping(method = RequestMethod.GET, value = "address")
	public Reaction address(String level) {
		//省市两层
		if ("2".equals(level)){
			return new ReactionImpl(AddressConstant.address_province_city);
		}
		return new ReactionImpl(AddressConstant.address.getJSONArray("data"));
	}
}
